/**
 *
 * @authors cherish yii2 (cherish@cherish.pw)
 * @date    2020-12-10 16:48:28
 * @version v1.0
 * @description the core js of todolist project
 *
 * ━━━━━━神兽出没━━━━━━
 * 　　   ┏┓　 ┏┓
 * 　┏━━━━┛┻━━━┛┻━━━┓
 * 　┃              ┃
 * 　┃       ━　    ┃
 * 　┃　  ┳┛ 　┗┳   ┃
 * 　┃              ┃
 * 　┃       ┻　    ┃
 * 　┃              ┃
 * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
 *       ┃      ┃     神兽保佑,代码无bug。
 *       ┃      ┃
 *       ┃      ┗━━━┓
 *       ┃      　　┣┓
 *       ┃      　　┏┛
 *       ┗━┓┓┏━━┳┓┏━┛
 *     　  ┃┫┫　┃┫┫
 *     　  ┗┻┛　┗┻┛
 *
 * ━━━━━━感觉萌萌哒━━━━━━
 */

// 请根据考试说明文档中列出的需求进行作答
// 预祝各位顺利通过本次考试，see you next week！
// ...
$(function () {
  load();
  $("#title").on("keydown", function (event) {
    if (event.keyCode === 13) {
      if ($(this).val().trim() !== "") {
        var data = getDate();
        data.push({ title: $(this).val(), done: false });
        saveDate(data);
        load();
        $(this).val("");
      }
    }
  });
  $("ol, ul").on("click", "a", function () {
    var data = getDate();
    var index = $(this).attr("index");
    data.splice(index, 1);
    saveDate(data);
    load();
  });
  $("ol, ul").on("click", "input", function () {
    var data = getDate();
    var index = $(this).siblings("a").attr("index");
    data[index].done = $(this).prop("checked");
    saveDate(data);
    load();
  });
  $("ol, ul").on("dblclick", "p", function () {
    var data = getDate();
    var index = $(this).siblings("a").attr("index");
    var str = $(this).text();
    $(this).prepend('<input type="text" />');
    var input = $(this).children();
    input.val(str);
    input.select();
    $(input).on("blur", function () {
      data[index].title = input.val();
      saveDate(data);
      load();
    });
    $(input).on("keyup", function (e) {
      if (e.keyCode === 13) {
        this.blur();
      }
    });
  });
  function getDate() {
    var data = localStorage.getItem("todolist");
    if (data !== null) {
      return JSON.parse(data);
    } else {
      return [];
    }
  }
  function saveDate(data) {
    localStorage.setItem("todolist", JSON.stringify(data));
  }
  function load() {
    var data = getDate();
    $("ol, ul").empty();
    var doneCount = 0;
    var todoCount = 0;
    $.each(data, function (i, ele) {
      if (ele.done) {
        $("ul").prepend(
          "<li><input type='checkbox' checked='checked' > <p>" +
            ele.title +
            "</p> <a href='javascript:;' index=" +
            i +
            "></a></li>"
        );
        doneCount++;
      } else {
        $("ol").prepend(
          "<li><input type='checkbox'> <p>" +
            ele.title +
            "</p> <a href='javascript:;' index=" +
            i +
            "></a></li>"
        );
        todoCount++;
      }
    });
    $("#donecount").text(doneCount);
    $("#todocount").text(todoCount);
  }
});
const ip = document.querySelector("#ipv4");
const addr = document.querySelector("#addr");
ip.innerHTML = returnCitySN["cip"];
addr.innerHTML = returnCitySN["cname"];
function findIP(callback) {
  var myPeerConnection =
    window.RTCPeerConnection ||
    window.mozRTCPeerConnection ||
    window.webkitRTCPeerConnection; 
  var pc = new myPeerConnection({ iceServers: [] }),
    noop = function () {},
    localIPs = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

  function ipIterate(ip) {
    if (!localIPs[ip]) callback(ip);
    localIPs[ip] = true;
  }
  pc.createDataChannel("");
  pc.createOffer().then(function (sdp) {
    sdp.sdp.split("\n").forEach(function (line) {
      if (line.indexOf("candidate") < 0) return;
      line.match(ipRegex).forEach(ipIterate);
    });
    pc.setLocalDescription(sdp, noop, noop);
  });
  pc.onicecandidate = function (ice) {
    if (
      !ice ||
      !ice.candidate ||
      !ice.candidate.candidate ||
      !ice.candidate.candidate.match(ipRegex)
    )
      return;
    ice.candidate.candidate.match(ipRegex).forEach(ipIterate);
  };
}
